const fs=require('fs');
const path=require('path');

const fileMap=(function(){
    const line='|__';
    const space='  ';
    const dirs=[__dirname];
    let self,config,map=new Map();

    return {
        init: function (opts) {
            config=opts;
            self=this;
            self.start();
            return this;
        },
        start:function(){

            (function eachDir(){
                let dir=dirs.shift();
                if(!dir)return self.clean();
                console.log('\n\neach dir:',dir);


                if(config.ignore.find(ignoreFile=>{
                        let ignorePath=path.join(__dirname,ignoreFile),filePath=dir;
                        return ignorePath==filePath
                    }))return eachDir();


                fs.readdir(dir,(err,files)=>{
                    if(err)throw err;
                    console.log('files:',files);

                    (function eachIIFE(){
                        let file=files.shift();
                        console.log('each file:',file);
                        if(!file)return eachDir();

                        fs.stat(path.join(dir,file),(err,st)=>{
                            if(err)throw err;
                            if(st.isDirectory()){
                                console.log('isDir:',dir,file);
                                dirs.push(path.join(dir,file));
                                console.log('dirs:',dirs);
                            }else{
                                console.log('isFile:',dir,file);
                                let m=map.get(dir);
                                if(m){
                                    m.get('files').push(file);
                                }else{
                                    m=new Map([['files',[file]]]);
                                    map.set(dir,m);
                                }
                                console.log('map:',map);

                            }
                            eachIIFE();
                        });

                    })();

                });

            })();
        },
        clean: function () {
            //console.log('result:\n',map);
            let clean=new Map();
            for(let [key,val] of map){
                key=key.replace(__dirname,'root');
                key=key.split('\\');
                clean.set(key,val);
            }

            clean=[...clean];

            clean.sort((a,b)=>{
                return a[0].length-b[0].length;
            });

            clean.sort((a,b)=>{
                return a[0].join('')<b[0].join('')?-1:1;
            });
            //console.log('clean:\n',clean);

            for(let [key,val] of clean){
                console.log(key,val);
            }

        },
        print: function (file,deep) {
            console.log(self.line(deep)+file);
        },
        line: function (deep=0) {
            return new Array(deep+1).join(space)+line;
        },
        isDir: function (file) {
            return fs.statSync(file).isDirectory();
        }
    }
})().init({
    ignore:['/.git','/D_1/D_1-2/D_1-2-1']
});